package com.demo.cms.action.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.demo.Constants;
import com.demo.cms.model.Article;
import com.demo.cms.model.ArticleComment;
import com.demo.cms.service.ArticleCommentService;
import com.demo.cms.service.ArticleService;
import com.demo.common.NetUtils;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.model.Member;
import com.demo.core.model.Site;
import com.demo.core.model.Template;
import com.demo.core.model.User;
import com.demo.core.service.base.TemplateService;
import com.demo.security.access.model.SecurityUser;
import com.demo.security.util.SecurityUtil;
import com.demo.webapp.action.PageAction;
import com.demo.webapp.action.TemplateProvider;

/**
 * 文章评论Action
 * 前端页提交评论的Acttion
 * <br>根据文章ID对文章进行评论
 * @author ryuu.kk
 *
 */
public class CommentAction extends ClientBaseAction implements TemplateProvider {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -2799310408067210630L;

	/**
	 * 文章ID
	 */
	private Long articleId;
	
	/**
	 * 文章模型
	 */
	private Article article;
	
	/**
	 * 文章评论模型
	 */
	private ArticleComment articleComment;
	
	/**
	 * 模板ID
	 */
	private Long templateId;
	
	/**
	 * 模板模型
	 */
	private Template template;
	
	/**
	 * 是否支持匿名评论
	 */
	private boolean allowAnonymous;
	
	/**
	 * 是否需要审核
	 */
	private boolean isCheck;
	
	/**
	 * 评论列表
	 */
	private List<ArticleComment> commentList;
	
	/**
	 * 回复Action
	 * @return String
	 */
	@Override
	public String execute() {
		
		// 请求的前期处理
		super.prepare();
		
		Long id = getId();
		User user = getUser();
		Member member = getMember();
		
		// 匿名评论
		allowAnonymous = (Boolean) getConfiguration().get(Constants.ARTICLE_REPLY_ANONYMOUS);
		// 评论审核
		isCheck = (Boolean) getConfiguration().get(Constants.ARTICLE_REPLY_ANONYMOUS);
		// 不允许匿名访问
		if (!allowAnonymous && user == null) {
			addActionError(getText("请您先登录后进行评论."));
			return CALLBACK;
		}
	
		if (user == null) {
			user = new User();
		} else {
			user.setPassword("");
		}
		if (allowAnonymous) {
			user.setUserName(Constants.USER_DEFAULT_NICK_NAME);
		} else {
			if (member != null) {
				user.setUserName(member.getNickName());
			} else {
				user.setUserName(user.getUserName() + "【非会员】");
			}
		}
		
		// 判断文章是否存在
		if (getArticleService().exists(articleId)) {
			// 用户的ip
			String ipAddress = NetUtils.getIpAddr(getRequest());
			String sendRegion = NetUtils.getRegion(getRequest());
			//SecurityUser user = SecurityUtil.getCurrentUser();
			if (user != null) {
				articleComment.setSendId(user.getId());
				//articleComment.setSendName(user.);
			}
			articleComment.setSendName(user.getUserName());
			Site site = getSite();
			if (site != null) {
				articleComment.setSiteId(site.getId());
			}
			articleComment.setIpAddress(ipAddress);
			articleComment.setSendRegion(sendRegion);
			// 文章存在,可以评论
			// 设置文章id
			articleComment.setArticleId(articleId);
			// 可读写删除
			articleComment.setStatus("RWD");
			// 类型：评论
			articleComment.setType(ArticleComment.TYPE_COMMENT);
			if (isCheck) {
				// 未通过审核
				articleComment.setCheckStep(ArticleComment.STEP_UNCHECKED);
			} else {
				// 审核通过
				articleComment.setCheckStep(ArticleComment.STEP_CHECKED);
			}
			
			// 设置审核通过,如果需要审核,则默认审核不通过
			articleComment.setChecked(!isCheck);
			// 是否是评论的一个回复
			if (id != null) {
				if (getArticleCommentService().exists(id)) {
					articleComment.setPid(id);
				}
//				ArticleComment acomment = articleCommentService.find(id);
//				if (acomment != null) {
//					articleComment.setPid(id);
//				}
			}
			getArticleCommentService().save(articleComment);
			// 判断是否需要审核
			if (articleComment.isChecked()) {
				addActionMessage(getText("您的评论目前正在审核,请稍后查看."));
			} else {
				addActionMessage(getText("感谢您的评论."));
			}
		} else {
			addActionError(getText("文章不存在,无法评论"));
		}
		
		return CALLBACK;
	}
	
	/**
	 * 评论列表
	 * @return
	 */
	public String list() {
		
		// 请求的前期处理
		super.prepare();
				
		if (templateId == null) {
			return CALLBACK;
		}
		Site site = getSite();
		Long siteId = site.getId();

		article = getArticleService().find(articleId);
		if (article == null) {
			addActionError(getText("文章不存在."));
			return CALLBACK;
		}
		
		setChannelPath(article.getChannelPath());
		setCategoryPath(article.getCategoryPath());
		
		ResultSearchResult<ArticleComment> resultSupport = getArticleCommentService().search(siteId, articleId, null, getArgumentSupport());
		commentList = new ArrayList<ArticleComment>(resultSupport.getResults());
		super.setResultSupport(resultSupport);
		
		// 取得渲染模板
		template = findTemplate(templateId);
		if (template == null) {
			return CALLBACK;
		} else {
			return SUCCESS;
		}
	}
	
	// ~~ setter here ============================
	
	public Long getArticleId() {
		return articleId;
	}

	public void setArticleId(Long articleId) {
		this.articleId = articleId;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticleComment(ArticleComment articleComment) {
		this.articleComment = articleComment;
	}

	public ArticleComment getArticleComment() {
		return articleComment;
	}

	public Long getTemplateId() {
		return templateId;
	}

	public void setTemplateId(Long templateId) {
		this.templateId = templateId;
	}

	public List<ArticleComment> getCommentList() {
		return commentList;
	}

	@Override
	public Template getTemplateModel() {
		return this.template;
	}

	@Override
	String getActionType() {
		return null;
	}
}
